#!/bin/bash

source /home/shell/util.sh

function install () {
    printInfo "当前Git版本是：`git --version`"
    while [ true ]; do
        printInfo "版本列表"
        curl -sL https://hub.fastgit.org/git/git/tags|grep "<a href=\"/git/git/releases/tag"|perl -pe "s/.*\/v(.*)\">.*/\1/g"
        read -p "请选择版本：" version
        if [[ -z "${version}" ]]; then
            printFail "请输入版本"
        else
            break;
        fi
    done
    download /usr/local/src/git.tar.gz https://hub.fastgit.org/git/git/archive/refs/tags/v${version}.tar.gz
    tar -xf /usr/local/src/git.tar.gz -C /usr/local/src/
    rm -rf /usr/local/src/git.tar.gz
    mv /usr/local/src/git-* /usr/local/src/git
    if [ "$(ostype)" == "Ubuntu" ]; then
        printWarn "Ubuntu系统"
        # apt-get -y --purge remove git
        apt install -y zlib1g zlib1g-dev asciidoc xmlto libcurl4-openssl-dev
    else
        printWarn "CentOS系统"
        # yum erase -y git
        yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker asciidoc xmlto
    fi
    if [ ! -d "/usr/etc" ]; then
        mkdir -p /usr/etc
    fi
    touch /usr/etc/gitconfig
    cd /usr/local/src/git
    make configure
    ./configure --prefix=/usr
    make all
    make install
    rm -rf /usr/local/src/git
    printInfo "===============================Git==============================="
    printInfo "git版本：`git --version`"
    printInfo "===============================Git==============================="
    printWarn "需要重新登录以生效！"
}

function config () {
    printInfo "开始配置Git。"
    /usr/bin/git config --system --unset credential.helper
    /usr/bin/git config --global credential.helper store
    /usr/bin/git config --global user.name "江路"
    /usr/bin/git config --global user.email jlcon@qq.com
    /usr/bin/git config --global core.longpaths true
    /usr/bin/git config --global http.sslVerify false
    /usr/bin/git config --global pull.rebase false

    printInfo "===============================Git==============================="
    printInfo "git版本：`git --version`"
    printInfo "===============================Git==============================="
    printWarn "需要重新登录以生效！"
}

install